package com.systemsetup.systemsetup.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.systemsetup.systemsetup.model.User;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @ClassName jwtUtils
 * @Author LiHaoDong
 * @Date 2020/7/2416:19
 * @Description TODO
 */
public class jwtUtils {
    //秘钥
    private static String SECRET = "u2wGsKqB2KyLuHGSCOSsLukxDmvEjxZE";

    //创建短token
    public static String createShortToken(User user) throws UnsupportedEncodingException {
        //秘钥HMAC256,加密
        Algorithm algorithm = Algorithm.HMAC256(SECRET);

        // 头部信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        //设置过期时间
        Date expireDate = new Date();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, 6);
        expireDate = cal.getTime();


        //生成token
        String token = "";
        if (user.getType() == 1) {
            token = JWT.create().withHeader(map)
                    /* 设置 载荷 Payload */
                    .withClaim("id",user.getId() )
                    .withClaim("home_id", user.getHomeId())
                    .withClaim("type", user.getType())
                    .withClaim("reginid", user.getStr("reginid"))
                    .withClaim("isUse", user.getIsUse())
                    .withExpiresAt(expireDate)// 签名过期的时间
                    /* 签名 Signature */
                    .sign(algorithm);
        }else{
            token = JWT.create().withHeader(map)
                    /* 设置 载荷 Payload */
                    .withClaim("id",user.getId() )
                    .withClaim("home_id", user.getHomeId())
                    .withClaim("type", user.getType())
                    .withClaim("isUse", user.getIsUse())
                    .withExpiresAt(expireDate)// 签名过期的时间
                    /* 签名 Signature */
                    .sign(algorithm);
        }

        return token;
    }

    //创建长token
    public static String createLongtToken(User user) throws UnsupportedEncodingException {
        //秘钥HMAC256,加密
        Algorithm algorithm = Algorithm.HMAC256(SECRET);

        // 头部信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        //设置过期时间
        Date expireDate = new Date();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, 24*30);
        expireDate = cal.getTime();
        //生成token
        String token = "";
        if (user.getType() == 1) {
            token = JWT.create().withHeader(map)
                    /* 设置 载荷 Payload */
                    .withClaim("id", user.getId())
                    .withClaim("home_id", user.getHomeId())
                    .withClaim("type", user.getType())
                    .withClaim("reginid", user.getStr("reginid"))
                    .withClaim("isUse", user.getIsUse())
                    .withExpiresAt(expireDate)// 签名过期的时间
                    /* 签名 Signature */
                    .sign(algorithm);
        }else{
            token = JWT.create().withHeader(map)
                    /* 设置 载荷 Payload */
                    .withClaim("id", user.getId())
                    .withClaim("home_id", user.getHomeId())
                    .withClaim("type", user.getType())
                    .withClaim("isUse", user.getIsUse())
                    .withExpiresAt(expireDate)// 签名过期的时间
                    /* 签名 Signature */
                    .sign(algorithm);
        }
        return token;
    }

    //验证短token
    public static DecodedJWT verifyShortToken(String access_token) throws UnsupportedEncodingException,TokenExpiredException {

        DecodedJWT jwt = null;
        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        JWTVerifier verifier = JWT.require(algorithm).build();
        jwt = verifier.verify(access_token);

       /* Map<String, Claim> claims = jwt.getClaims();
        for (Map.Entry<String, Claim> entry : claims.entrySet()) {
            String key = entry.getKey();
            Claim claim = entry.getValue();
        }
        String loginName = claims.get("loginName").asString();
        String userName = claims.get("userName").asString();
        String deptName = claims.get("deptName").asString();
        Map<String, Object> map = new HashMap<>();
        map.put("loginName",loginName);
        map.put("userName",userName);
        map.put("deptName",deptName);
        return RespEntity.success(map);*/
        return jwt;
    }

    //验证长token
    public static DecodedJWT verifyLongToken(String shortToken) throws TokenExpiredException,UnsupportedEncodingException{
        DecodedJWT jwt = null;

        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        JWTVerifier verifier = JWT.require(algorithm).build();
        jwt = verifier.verify(shortToken);

        /*Map<String, Claim> claims = jwt.getClaims();
        for (Map.Entry<String, Claim> entry : claims.entrySet()) {
            String key = entry.getKey();
            Claim claim = entry.getValue();
        }
        String loginName = claims.get("loginName").asString();
        String userName = claims.get("userName").asString();
        String deptName = claims.get("deptName").asString();
        Map<String, Object> map = new HashMap<>();
        map.put("loginName",loginName);
        map.put("userName",userName);
        map.put("deptName",deptName);
        return RespEntity.success(map);*/
        return jwt;
    }

    //取消长token
    public static String deleteLongtToken(User user) throws UnsupportedEncodingException {
        final String DELETESECRET = "deletescret";
        //秘钥HMAC256,加密
        Algorithm algorithm = Algorithm.HMAC256(DELETESECRET);

        // 头部信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        //设置过期时间
        Date expireDate = new Date();
        System.out.println("之前"+expireDate);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, 12);
        expireDate = cal.getTime();

        System.out.println("之后"+expireDate);

        //生成token
        String token = JWT.create().withHeader(map)
                /* 设置 载荷 Payload */
                .withClaim("id", user.getId())
                .withClaim("home_id", user.getHomeId())
                .withClaim("type", user.getType())
                .withExpiresAt(expireDate)// 签名过期的时间
                /* 签名 Signature */
                .sign(algorithm);
        return token;
    }

    //解析短token
    public static Map<String, Object> analysisShortToken(String shortToken) throws UnsupportedEncodingException,TokenExpiredException{

        DecodedJWT jwt = null;

        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        JWTVerifier verifier = JWT.require(algorithm).build();
        jwt = verifier.verify(shortToken);

        Map<String, Claim> claims = jwt.getClaims();
        for (Map.Entry<String, Claim> entry : claims.entrySet()) {
            String key = entry.getKey();
            Claim claim = entry.getValue();
        }
        String loginName = claims.get("id").asString();
        String userName = claims.get("home_id").asString();
        String deptName = claims.get("type").asString();
        Map<String, Object> map = new HashMap<>();
        map.put("loginName",loginName);
        map.put("userName",userName);
        map.put("deptName",deptName);
        return map;
    }
}

    
